package org.hackystat.ui.telemetryviewer.gwt.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Image;

/**
 * Implements the ChartImage widget. This widget draws the image itself by using JFreeChart servlet.
 *
 * @author Pavel Senin.
 *
 */
public class ChartImageWidget extends Image {

  /**
   * The service handler.
   */
  private final ChartGeneratorRPCAsync chartGeneratorService;
  private final ServiceDefTarget endpoint;
  private final String moduleRelativeURL;

  /**
   * The Chart image handler.
   *
   * @param type specifies the type of chart it could be "dependency" or "languages" so far.
   */
  public ChartImageWidget(String type) {
    super();

    /*
     * Setup connection to chart generator service.
     */
    chartGeneratorService = (ChartGeneratorRPCAsync) GWT.create(ChartGeneratorRPC.class);
    endpoint = (ServiceDefTarget) chartGeneratorService;
    moduleRelativeURL = GWT.getModuleBaseURL() + "chartGenerator";
    endpoint.setServiceEntryPoint(moduleRelativeURL);

    /*
     * Setup the callback from the chart generator service.
     */
    AsyncCallback callback = new AsyncCallback() {
      /*
       * If the call was successful, we will get back the name of the chart that was created and
       * stored on the server.
       */
      public void onSuccess(Object s) {
        String chartName = (String) s;
        setUrl(chartName);
      }

      /*
       * Something went wrong with the call. Handle the issue how you'd like.
       */
      public void onFailure(Throwable ex) {
        // do nothing for now
      }
    };

    /*
     * Make the call to the chart generator service with the previously created callback.
     */
    chartGeneratorService.generateChart(type, callback);

    /*
     * Since we've made an asynchronous call, we don't need to do anything further. The callback
     * object will handle the remainder of the work for us.
     */
  }

  /**
   * Updates the chart image according the request.
   *
   * @param type the new chart specification.
   */
  public void update(String type) {

    /*
     * Setup the callback from the chart generator service.
     */
    AsyncCallback callback = new AsyncCallback() {
      /*
       * If the call was successful, we will get back the name of the chart that was created and
       * stored on the server.
       */
      public void onSuccess(Object s) {
        String chartName = (String) s;
        setUrl(chartName);
      }

      /*
       * Something went wrong with the call. Handle the issue how you'd like.
       */
      public void onFailure(Throwable ex) {
        // do nothing for now
      }
    };

    /*
     * Make the call to the chart generator service with the previously created callback.
     */
    chartGeneratorService.generateChart(type, callback);

    /*
     * Since we've made an asynchronous call, we don't need to do anything further. The callback
     * object will handle the remainder of the work for us.
     */

  }

  /**
   * Reports latest server response.
   *
   * @return the server response.
   */
  public String getLatestResponse() {
    return (super.getUrl());
  }
}